﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.Statistics;
using MINERP.MODEL.FoundationModel;
using FastReport;

namespace MINERP.Statistics.ShopBill
{
    public partial class FrmUnCompleteCount : Form
    {
        public FrmUnCompleteCount()
        {
            InitializeComponent();
        }

        private static FrmUnCompleteCount _instance;
        public static FrmUnCompleteCount GetInstance
        {
            get
            {
                if (_instance == null || _instance.IsDisposed)
                {
                    _instance = new FrmUnCompleteCount();
                }
                return _instance;
            }
        }
        BillIndexBLL2 billIndexBll = new BillIndexBLL2();
        SHOP_Bill_Info shopBill = new SHOP_Bill_Info();
        int currShopID = 0;
        private void backgroundWorkerGetData_DoWork(object sender, DoWorkEventArgs e)
        {
            _isRunning = true;
            try
            {
                shopBill = billIndexBll.GetUnCompleteProductBillByShop(dateTimePickerStartDate.Value.Date, dateTimePickerEndDate.Value.Date, currShopID);
                backgroundWorkerGetData.ReportProgress(100,"");
            }catch(Exception exxx)
            {
                backgroundWorkerGetData.ReportProgress(-1, exxx.Message);
            }
            _isRunning = false;
        }

        private void backgroundWorkerGetData_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.ProgressPercentage == -1)
            {
                MessageBox.Show((string)e.UserState);
            }
            else if (e.ProgressPercentage == 100)
            {
                ShowErrMsg("处理完成");
                if (shopBill.List_Detail.Count != 0)
                {
                    PrintBill(shopBill);
                }

                else
                {
                    ShowErrMsg("未找到结果");
                }
                //此处进行打印
            }
            else
            { }
        }

        private void backgroundWorkerGetData_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

            ShowErrMsg("处理完成");
            if (shopBill.List_Detail.Count != 0)
            {
                PrintBill(shopBill);
            }

            else
            {
                ShowErrMsg("未找到结果");
            }
            //此处进行打印
           
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            Shop_Info currShop = (Shop_Info)cbShop.SelectedItem;
            if (currShop != null&&!_isRunning)
            {
                progressBar1.Value = 0;
                iCount = 0;
                iCurr = 0;
                currShopID = currShop.ShopId;
                backgroundWorkerGetData.RunWorkerAsync();
               
            }
        }
        private void textEditHelpCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (Keys.Enter == e.KeyCode)
            {
                cbShop.DisplayMember = "ShopName";
                cbShop.ValueMember = "ShopId";
                cbShop.DataSource = billIndexBll.GetShopInfoByHelpCode(textEditHelpCode.Text.Trim());


            }
        }
        bool _isRunning = false;
        private void FrmUnCompleteCount_Load(object sender, EventArgs e)
        {
            billIndexBll.ShowMessage += new BillIndexBLL2.ShowMsgEventHandler(billIndexBll_ShowMessage);
        }
        int iCount, iCurr = 0;
        void billIndexBll_ShowMessage(int num, int type)
        {
            //进度条 及文本显示
            if (type == 0)
            {
                iCount = num;

            }
            else
            {
                iCurr = num;

            }
            ShowErrMsg("正在处理......" + iCurr.ToString() + "/" + iCount.ToString());
            ShowProgressStatus(iCount, 0, iCurr);
        }
        public delegate void ShowErrMsgHander(string msg);
        public void ShowErrMsg(string msg)
        {
            if (labelShowMsg.InvokeRequired)
            {
                ShowErrMsgHander showMsg = new ShowErrMsgHander(ShowErrMsg);
                this.Invoke(showMsg, new object[] { msg });
            }
            else
            {
                labelShowMsg.Text = msg;
            }
        }
        public delegate void ShowStatusMsgHander(int max,int min,int curr);
        public void ShowProgressStatus(int max, int min, int curr)
        {
            if (labelShowMsg.InvokeRequired)
            {
                this.Invoke(new ShowStatusMsgHander(ShowProgressStatus), new object[] { max, min, curr });
            }
            else
            {
                progressBar1.Minimum = 0;
                progressBar1.Maximum = iCount;
                progressBar1.Value = iCurr;
            }
        }
        private Report report = new Report();
        /// <summary>
        /// 打印帐单
        /// </summary>
        /// <param name="sbinfo"></param>
        public void PrintBill(SHOP_Bill_Info sbinfo)
        {
            report.Load(Application.StartupPath + "\\Report\\PrintTicket.frx");

          
            #region 组织打印数据

            bool IsNeedPrintMoney = true;//这里是预估价格，无论怎样都要打印价格出来
            DataTable tableTitle = CreateShopBillTitle();
            DataRow titleRow = tableTitle.NewRow();
            titleRow["ShopName"] = sbinfo.Shop.ShopName;
            titleRow["Tel1"] = sbinfo.Shop.Tel1;
            titleRow["Tel2"] = sbinfo.Shop.Tel2;
            titleRow["ShopId"] = sbinfo.Shop.ShopId;
            titleRow["shopKeeper"] = sbinfo.Shop.ShopKeeper;
            titleRow["QQ"] = sbinfo.Shop.QQ;
            titleRow["BillId"] = sbinfo.BillId;
            titleRow["BillOrderCode"] = sbinfo.BillOrderCode;
            titleRow["CreateTime"] = sbinfo.CreateTime;
            if (IsNeedPrintMoney)
            {
                titleRow["Balance"] = sbinfo.ShopBalance;
                titleRow["BillCount"] = sbinfo.BillCount;
            }
            else
            {
                titleRow["Balance"] = 0;
                titleRow["BillCount"] = 0;
            }


            titleRow["Salesman"] = sbinfo.Salesman;
            titleRow["CreditLevel"] = sbinfo.CreditLevel == null ? 0 : sbinfo.CreditLevel;
            tableTitle.Rows.Add(titleRow);

            tableTitle.TableName = "tableTitle";


            DataTable tableDetail = CreateShopBillDetail();
            DataTable tableSubDetail = CreateShopBillSubDetail();
            List<Bill_Detail_Info> detailList = sbinfo.List_Detail.ToList();

            foreach (Bill_Detail_Info billDetail in detailList)
            {
                DataRow row = tableDetail.NewRow();
                row["BillDetailId"] = billDetail.BillDetailId;
                row["OrderInfoCode"] = billDetail.OrderInfoCode;
                row["OrderProductName"] = billDetail.OrderProductName;
                row["CtmName"] = billDetail.CtmName;
                row["Remark"] = billDetail.Remark;
                row["OrderBagName"] = billDetail.OrderBagName;

                tableDetail.Rows.Add(row);
                List<Bill_Detail_Detail_Info> subDetail = billDetail.ListDetail_Detail.ToList();
                foreach (Bill_Detail_Detail_Info detail in subDetail)
                {
                    DataRow rowSub = tableSubDetail.NewRow();
                    rowSub["DetailName"] = detail.DetailName;
                    rowSub["BillDetailId"] = billDetail.BillDetailId;
                    rowSub["Spec"] = detail.Spec;
                    rowSub["Num"] = detail.Num;
                    rowSub["UnitPrice"] = detail.UnitPrice;
                    rowSub["DetailRemark"] = detail.DetailRemark;
                    if (IsNeedPrintMoney)
                    {
                        rowSub["Count"] = detail.Count;
                    }
                    else
                    {
                        rowSub["Count"] = 0;
                    }

                    rowSub["Area"] = detail.Area;
                    rowSub["Type"] = detail.Type;
                    tableSubDetail.Rows.Add(rowSub);
                }
            }

            tableDetail.TableName = "tableDetail";
            tableSubDetail.TableName = "tableSubDetail";
            #endregion

            #region 打印报表
            DataSet set = new DataSet("ds");
            set.Tables.Add(tableTitle);
            set.Tables.Add(tableDetail);
            set.Tables.Add(tableSubDetail);

            set.WriteXml(@"C:\Windows\xmldata\PrintTicket.xml", XmlWriteMode.WriteSchema);
            report.RegisterData(tableTitle, "tableTitle");
            report.RegisterData(tableDetail, "tableDetail");
            report.RegisterData(tableSubDetail, "tableSubDetail");
            report.Preview = previewControl1;
            report.Show();
            //report.Print();
            #endregion



        }
        /// <summary>
        /// 创建明细产品
        /// </summary>
        /// <returns></returns>
        private DataTable CreateShopBillDetail()
        {
            DataTable table = new DataTable();
            table.Columns.Add("BillDetailId", typeof(int));
            table.Columns.Add("OrderInfoCode", typeof(string));
            table.Columns.Add("OrderProductName", typeof(string));
            table.Columns.Add("CtmName", typeof(string));
            table.Columns.Add("Remark", typeof(string));
            table.Columns.Add("OrderBagName", typeof(string));
            return table;
        }
        /// <summary>
        /// 创建明细收费项目表
        /// 
        /// </summary>
        /// <returns></returns>
        private DataTable CreateShopBillSubDetail()
        {
            DataTable table = new DataTable();
            table.Columns.Add("BillDetailId", typeof(int));
            table.Columns.Add("DetailName", typeof(string));
            table.Columns.Add("Spec", typeof(string));
            table.Columns.Add("Num", typeof(int));
            table.Columns.Add("UnitPrice", typeof(double));
            table.Columns.Add("Count", typeof(double));
            table.Columns.Add("Area", typeof(double));
            table.Columns.Add("Type", typeof(int));
            table.Columns.Add("DetailRemark", typeof(string));
            return table;
        }
        /// <summary>
        /// 创建发货信息表
        /// </summary>
        /// <returns></returns>
        private DataTable CreateFahuoTable()
        {
            DataTable table = new DataTable("tableBottom");
            table.Columns.Add("ConsigneeName", typeof(string));
            table.Columns.Add("ConsigneeTelePhone", typeof(string));
            table.Columns.Add("DistributionAddress", typeof(string));
            table.Columns.Add("BusTimetable", typeof(string));//收货单位
            table.Columns.Add("ReMark", typeof(string));
            table.Columns.Add("DistributionMode", typeof(string));
            table.Columns.Add("Fahuodanwei", typeof(string));
            table.Columns.Add("Fahuodianhua", typeof(string));

            return table;
        }
        /// <summary>
        /// 创建title表
        /// </summary>
        /// <returns></returns>
        private DataTable CreateShopBillTitle()
        {
            DataTable table = new DataTable();
            table.Columns.Add("ShopName", typeof(string));
            table.Columns.Add("Tel1", typeof(string));
            table.Columns.Add("Tel2", typeof(string));
            table.Columns.Add("ShopId", typeof(int));
            table.Columns.Add("BillOrderCode", typeof(string));
            table.Columns.Add("shopKeeper", typeof(string));
            table.Columns.Add("QQ", typeof(string));
            table.Columns.Add("BillId", typeof(string));
            table.Columns.Add("CreateTime", typeof(DateTime));
            table.Columns.Add("Balance", typeof(double));
            table.Columns.Add("BillCount", typeof(double));
            table.Columns.Add("CreditLevel", typeof(int));
            table.Columns.Add("Salesman", typeof(string));
            return table;

        }
    }
}
